<?php

namespace ApiBundle\Controller\Admin;

use ApiBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class SettingController extends BaseController
{
    /*
     * 编辑/添加企业类别
     */
    public function editCompanyCategoryAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        $conn = $this->get("database_connection");
        $id = $request->get("id");
        $category_name = $request->get("category_name");
        if(empty($category_name)){
            return $this->jsonResponse(1,"参数错误");
        }

        // 编辑时，获取原勋章名称
        $origin_name = $conn->fetchAssoc("SELECT * FROM company_category WHERE id = '". $id ."'");
        if($origin_name['company_category_name'] != $category_name){

            // 判断企业类别是否存在
            $name = $conn->fetchAssoc("SELECT * FROM company_category WHERE company_category_name = '". $category_name ."'");

            if($name){
                return $this->jsonResponse(1,"该企业类别已存在");
            }
        }



        $data = array(
            "company_category_name" => $category_name
        );

        if($id == "0" || $id == ""){
            $conn->insert("company_category",$data);
        } else {
            $where = array(
                "id" => $id
            );
            $conn->update("company_category",$data,$where);
        }


        return $this->jsonResponse(0,"操作成功",$data);

    }

    /*
     * 删除类别
     */
    public function deleteCompanyCategoryAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        $id = $request->get("id");

        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        if($id == "1"){
            return $this->jsonResponse(1,"该分类不能删除");
        }

        $conn = $this->get("database_connection");

        // 判断该分类下是否有数据
        $company = $conn->fetchAll("SELECT * FROM company WHERE company_category_id = '". $id ."' ");
        if($company){
            foreach($company as $item){

                $conn->update("company",array("company_category_id" => "1"),array("id" => $item['id']));
            }
        }

        $conn->delete("company_category",array("id" => $id));

        return $this->jsonResponse(0,"操作成功");

    }

    /*
     * 获取企业类别列表
     */

    public function companyCategoryListAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $page = $request->get("page","1");

        $rows = $request->get("rows","10");

        $conn = $this->get("database_connection");

        $sql = "SELECT * FROM company_category WHERE 1 ";

        // 搜索
        $category_name = $request->get("category_name");
        if(!empty($category_name)){
            $sql .= " AND company_category_name = '". $category_name ."' ";
        }

        $sql .= " ORDER BY id DESC";

        $search_all_count = count($conn->fetchAll($sql));

        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page -1) * $rows;

        $sql .= " LIMIT $m,$rows ";


        $data = $conn->fetchAll($sql);

        return $this->jsonResponse(0,"操作成功",array("data" => $data,"search_all_count"=>$search_all_count,"all_page"=>$all_page));
    }



    /*
     * 编辑/添加企业属性
     */
    public function editCompanyAttributeAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        $conn = $this->get("database_connection");
        $id = $request->get("id");
        $attribute_name = $request->get("attribute_name");
        if(empty($attribute_name)){
            return $this->jsonResponse(1,"参数错误");
        }


        // 编辑时，获取原属性名称
        $origin_name = $conn->fetchAssoc("SELECT * FROM company_attribute WHERE id = '". $id ."'");
        if($origin_name['company_attribute_name'] != $attribute_name){

            // 判断企业属性是否存在
            $name = $conn->fetchAssoc("SELECT * FROM company_attribute WHERE company_attribute_name = '". $attribute_name ."'");

            if($name){
                return $this->jsonResponse(1,"该企业属性已存在");
            }
        }

        $data = array(
            "company_attribute_name" => $attribute_name
        );

        if($id == "0" || $id == ""){
            $conn->insert("company_attribute",$data);
        } else {
            $where = array(
                "id" => $id
            );
            $conn->update("company_attribute",$data,$where);
        }


        return $this->jsonResponse(0,"操作成功",$data);

    }

    /*
     * 删除属性
     */
    public function deleteCompanyAttributeAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        $id = $request->get("id");

        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        if($id == "1"){
            return $this->jsonResponse(1,"该属性不能删除");
        }

        $conn = $this->get("database_connection");

        // 判断该分类下是否有数据
        $company = $conn->fetchAll("SELECT * FROM company WHERE company_attribute_id = '". $id ."' ");
        if($company){
            foreach($company as $item){

                $conn->update("company",array("company_attribute_id" => "1"),array("id" => $item['id']));
            }
        }

        $conn->delete("company_attribute",array("id" => $id));

        return $this->jsonResponse(0,"操作成功");

    }

    /*
     * 获取企业属性列表
     */

    public function companyAttributeListAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $page = $request->get("page","1");

        $rows = $request->get("rows","10");

        $conn = $this->get("database_connection");
        $sql = "SELECT * FROM company_attribute WHERE 1";

        // 搜索
        $attribute_name = $request->get("attribute_name");
        if(!empty($attribute_name)){
            $sql .= " AND company_attribute_name = '". $attribute_name ."' ";
        }

        $sql .= " ORDER BY id DESC";

        $search_all_count = count($conn->fetchAll($sql));

        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page -1) * $rows;

        $sql .= " LIMIT $m,$rows ";
        $data = $conn->fetchAll($sql);

        return $this->jsonResponse(0,"操作成功",array("data" => $data,"search_all_count"=>$search_all_count,"all_page"=>$all_page));
    }

    /*
     * 申请  科技联络人列表
     */
    public function technologyLiaisonsListAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        $page = $request->get("page","1");

        $rows = $request->get("rows","10");

        $conn = $this->get("database_connection");
//        $user = $conn->fetchAll("SELECT * FROM user WHERE technology_liaisons_status = 0 AND company_id IN (SELECT id FROM company WHERE admin_id = '". $this->getAdminId() ."')");


        $admin_role_id = $this->get("session")->get("admin_role_id");

        if($admin_role_id == 4){

            $sql = "SELECT * FROM user WHERE technology_liaisons_status = 0 AND company_id IN (SELECT id FROM company WHERE admin_id = '". $this->getAdminId() ."')";
        } else {

            $sql = "SELECT * FROM user WHERE technology_liaisons_status = 0 AND district_id = '". $this->getDistrictId() ."'";
        }


        // 搜索条件下总数
        $search_all_count = count($conn->fetchAll($sql));

        $all_page = ceil($search_all_count / $rows);


        // 待审核科技联络人总数
        $user_count = count($conn->fetchAll($sql));

        //科技联络人总数
        $teac_sql = "SELECT * FROM user WHERE technology_liaisons_status = 1 AND district_id = '". $this->getDistrictId() ."'";

        $is_teac_count = count($conn->fetchAll($teac_sql));

        // 已拒绝总数
        $refuse_sql = "SELECT * FROM user WHERE technology_liaisons_status = -1 AND district_id = '". $this->getDistrictId() ."'";

        $is_refuse = count($conn->fetchAll($refuse_sql));

        // 分页
        $m = ($page -1) * $rows;

        $sql .= " LIMIT $m,$rows ";

        $user = $conn->fetchAll($sql);

        return $this->jsonResponse(0,"操作成功",array("data"=>$user,"check_count" => $user_count,"is_teac_count"=>$is_teac_count,"is_refuse"=>$is_refuse,"search_all_count"=>$search_all_count,"all_page"=>$all_page));
    }

    /*
     * 审核 科技联络人
     */
    public function technologyLiaisonsAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $id = $request->get("id");
        $status = $request->get("status"); // 1 同意 -1拒绝 2删除
        $company_id = $request->get("company_id");
        $conn = $this->get("database_connection");

        if(empty($id) || empty($status)){
            return $this->jsonResponse(1,"参数错误");
        }

        // 判断科技联络人是否已满
        if($status == "1"){
            $count = $conn->fetchColumn("SELECT count(*) FROM user WHERE company_id = '". $company_id ."' AND technology_liaisons_status = 1");


            if($count == 3){

                $data = array(
                    "technology_liaisons_status" => "2"
                );

                //$conn->update("user",$data,array("id" => $id));
                return $this->jsonResponse(1,"科技联络人人数已有三位，审核不通过");
            }
        }

        $data = array(
            "technology_liaisons_status" => $status
        );

        $conn->update("user",$data,array("id" => $id));

        return $this->jsonResponse(0,"操作成功");
    }

    /*
     * 编辑/添加banner
     */
    public function editBannerAction(Request $request)
    {
        $role = $request->get("role"); // 1.平台管理员  2.超级管理员

        if($role == 1){

            if(!$this->isPlatAdminLogged()){
                return $this->plat_response;
            }

        } else if($role == 2){

            if(!$this->isAdminLogged()){
                return $this->admin_response;
            }

        }

        $id = $request->get("id");
        $is_company = $request->get("is_company"); // 1企业端 0管理员端
        $name = $request->get("name");
        $url = $request->get("url");
        $image_path = $request->get("image");



        if(empty($name) || empty($image_path)){
            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");

        // 当前管理员id
        $admin = $conn->fetchAssoc("SELECT * FROM admin WHERE id = '". $this->getAdminId() ."'");

        if($admin['admin_role_id'] == "3" || $admin['admin_role_id'] == "4"){
            return $this->jsonResponse(1,"权限不足",$admin['admin_role_id']);
        }


        $data = array(
            "image_path" => $image_path,
            "url" => $url,
            "name" => $name,
            "admin_role_id" => $role
        );

        if(empty($id)){
            $data['is_company'] = $is_company;
            $data['district_id'] = $this->get("session")->get("district_id");

            // 判断添加数量是否超过3张

            $banner_sql = "SELECT count(*) FROM banner WHERE is_company = '". $is_company ."' AND admin_role_id = '". $data['admin_role_id'] ."'";

            if($role == 2){
                $banner_sql .= " AND district_id = '". $this->get("session")->get("district_id") ."' ";
            }
            $banner_count = $conn->fetchColumn($banner_sql);


            if($banner_count == "3"){
                return $this->jsonResponse(1,"banner数量已达3张，不可添加");
            }

            $conn->insert("banner",$data);
        } else {

            // 判断$where 删除条件是否存在
            $banner = $conn->fetchAssoc("SELECT * FROM banner WHERE id = '". $id ."'");

            if($banner['admin_role_id'] != $role){
                return $this->jsonResponse(1,"权限不足",array("banner_role"=>$banner['admin_role_id'],"admin_role"=>$role));
            }


            $where = array(
                "id" => $id
            );
            $conn->update("banner",$data,$where);
        }
        return $this->jsonResponse(0,"操作成功");
    }

    /*
     * 获取banner列表  超级管理员/平台管理员管理 非展示
     */
    public function getBannerListAction(Request $request)
    {
        $conn = $this->get("database_connection");
        $role = $request->get("role"); // 1.平台管理员  2.超级管理员

        if($role == 1){

            if(!$this->isPlatAdminLogged()){
                return $this->plat_response;
            }

        } else if($role == 2){

            if(!$this->isAdminLogged()){
                return $this->admin_response;
            }

        }
        
        $page = $request->get("page","1");

        $rows = $request->get("rows","10");

//        // 当前管理员role_id
//        $data = $conn->fetchAssoc("SELECT admin_role_id FROM admin WHERE id = '". $this->getAdminId() ."'");
//
//
//        if($data['admin_role_id'] == "3" || $data['admin_role_id'] == "4"){
//            return $this->jsonResponse(1,"权限不足");
//        }

        if($role == 2){

            $sql = "SELECT * FROM banner WHERE admin_role_id = 2 AND district_id = ". $this->getDistrictId();

        } else if($role == 1){

            $sql = "SELECT * FROM banner WHERE admin_role_id = 1";

        } else {
            return $this->jsonResponse(1,"权限不足");
        }


        //搜索条件下的总数
        $search_all_count = count($conn->fetchAll($sql));

        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page -1) * $rows;

        $sql .= " LIMIT $m,$rows ";


        $data = $conn->fetchAll($sql);

        return $this->jsonResponse(0,"操作成功",array("banner" => $data,"search_all_count"=>$search_all_count,"all_page"=>$all_page));
    }

    /*
     * 删除banner
     */
    public function deleteBannerAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $conn = $this->get("database_connection");
        $id = $request->get("id");

        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        // 当前管理员role_id
        $data = $conn->fetchAssoc("SELECT admin_role_id FROM admin WHERE id = '". $this->getAdminId() ."'");

        if($data['admin_role_id'] == "3" || $data['admin_role_id'] == "4"){
            return $this->jsonResponse(1,"权限不足");
        }


        $where = array(
            "id" => $id,
            "admin_role_id" => $data['admin_role_id']
        );

        // 判断$where 删除条件是否存在
        $banner = $conn->fetchAssoc("SELECT * FROM banner WHERE id = '". $id ."'");
        if($banner['admin_role_id'] != $data['admin_role_id']){
            return $this->jsonResponse(1,"权限不足",array("banner_role"=>$banner['admin_role_id'],"admin_role"=>$data['admin_role_id']));
        }

        $conn->delete("banner",$where);
        return $this->jsonResponse(0,"操作成功");
    }

    /*
     * 注册协议  编辑
     */
    public function agreementAction(Request $request)
    {
        $content = $request->get("content");

        $conn = $this->get("database_connection");

        $is_exit = $conn->fetchColumn("SELECT count(*) FROM settings WHERE type = 2");

        if($is_exit){
            $conn->update("settings",array("config_value" => $content, "config_key" => "agreement"),array("type" => "2"));
        } else {

            $conn->insert("settings",array("config_value" => $content, "config_key" => "agreement", "type" => "2"));
        }

        return $this->jsonResponse(0,"操作成功");

    }

    /*
     * 获取注册协议
     */
    public function getAgreementAction()
    {
        $conn = $this->get("database_connection");
        $data = $conn->fetchAssoc("SELECT * FROM settings WHERE type = 2");
        return $this->jsonResponse(0,"操作成功",array("data"=>$data));
    }

    /*
     * 小程序 banner展示
     */
    public function getAdminBannerAction(){
        $conn = $this->get("database_connection");
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $data = $conn->fetchAll("SELECT * FROM banner WHERE is_company = 0 AND (district_id = '". $this->getDistrictId() ."' OR admin_role_id = 1 ) ");

        return $this->jsonResponse(0,"操作成功",$data);

    }

}
